package com.nannybear.Controller;


import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.nannybear.MainActivity;
import com.nannybear.R;
import com.nannybear.View.Screen;

/**
 * @brief puppeteer who manage the views change
 */
public class ViewPuppeteer {
    /********** DECLARATIONS **********/
	private MainActivity mActivity;
	private ParametersController parametersController;
	private GraphsDrawer graphsDrawer;
    private TimelineController timelineController;
    private CriticalityThresholds criticalityThresholds;

    /************ METHODS ************/
    /**
     * @brief constructor
     * @param activity : takes the activity
     */
	public ViewPuppeteer(MainActivity activity) {
		this.mActivity = activity;
		this.initInterface();
        this.addSpyButtons();
        this.parametersController = new ParametersController(this);
        this.graphsDrawer = new GraphsDrawer(this);
        this.timelineController = new TimelineController(this);
        this.criticalityThresholds = new CriticalityThresholds(this);
       
        this.displayCurrentView();
	}

    /**
     * @brief initialize all the layout and its widget if necessary
     */
	private void initInterface(){
		// Home screen
		this.mActivity.getHomeScreen().setHome_screen_layout((RelativeLayout) this.mActivity.findViewById(R.id.home_screen_layout));
		this.mActivity.getHomeScreen().setWarning_temp((ImageView) this.mActivity.findViewById(R.id.home_temp_warning));
		this.mActivity.getHomeScreen().setWarning_hum((ImageView) this.mActivity.findViewById(R.id.home_hum_warning));
        this.mActivity.getHomeScreen().setTemp_value((TextView) this.mActivity.findViewById(R.id.home_temp_value));
        this.mActivity.getHomeScreen().setLight_value((TextView) this.mActivity.findViewById(R.id.home_light_value));
        this.mActivity.getHomeScreen().setHumidity_value((TextView) this.mActivity.findViewById(R.id.home_hum_value));
        this.mActivity.getHomeScreen().setState_value((TextView) this.mActivity.findViewById(R.id.home_sound_value));
        
        // Temp Screen
		this.mActivity.getTempScreen().setWarning((ImageView) this.mActivity.findViewById(R.id.temp_warning));
		this.mActivity.getTempScreen().setTemp_screen_layout((RelativeLayout) this.mActivity.findViewById(R.id.temp_screen_layout));
        this.mActivity.getTempScreen().setTemp_value((TextView) this.mActivity.findViewById(R.id.temp_value));

        // Brightness screen
        this.mActivity.getBrightnessScreen().setLight_screen_layout((RelativeLayout) this.mActivity.findViewById(R.id.light_screen_layout));
        this.mActivity.getBrightnessScreen().setLight_bulb((ImageView) this.mActivity.findViewById(R.id.light_bulb));
        this.mActivity.getBrightnessScreen().setBrightness_value((TextView) this.mActivity.findViewById(R.id.light_value));

        // Humidity screen
        this.mActivity.getHumidityScreen().setWarning((ImageView) this.mActivity.findViewById(R.id.hum_warning));
		this.mActivity.getHumidityScreen().setHum_screen_layout((RelativeLayout) this.mActivity.findViewById(R.id.hum_screen_layout));
        this.mActivity.getHumidityScreen().setHum_value((TextView) this.mActivity.findViewById(R.id.hum_value));

        // Baby state screen
        this.mActivity.getBabyStateScreen().setBbstate_screen_layout((RelativeLayout) this.mActivity.findViewById(R.id.bbstate_screen_layout));
        
        // Settings screen
        this.mActivity.getSettingsScreen().setSettings_screen_layout((ScrollView) this.mActivity.findViewById(R.id.settings_screen_layout));
        
        // Help screen
        this.mActivity.getHelpScreen().setHelp_screen_layout((RelativeLayout) this.mActivity.findViewById(R.id.help_screen_layout));
    }
	
	/**
     * @brief Initialize all the tab buttons, add listener and add buttons to each view
     */
    public void addSpyButtons() {
    	//Initialization of the tab buttons
        ImageButton homeButton = (ImageButton) this.mActivity.findViewById(R.id.buttonHome);
        ImageButton tempButton = (ImageButton) this.mActivity.findViewById(R.id.buttonTemp);
        ImageButton brightnessButton = (ImageButton) this.mActivity.findViewById(R.id.buttonBrightness);
        ImageButton humidityButton = (ImageButton) this.mActivity.findViewById(R.id.buttonHum);
        ImageButton babyStateButton = (ImageButton) this.mActivity.findViewById(R.id.buttonSound);
        ImageButton settingsButton = (ImageButton) this.mActivity.findViewById(R.id.buttonParam);
        ImageButton helpButton = (ImageButton) this.mActivity.findViewById(R.id.buttonHelp);    	
    	
    	//listening tab buttons
        tempButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getTempScreen());
            }
        });
        homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getHomeScreen());
            }
        });
        brightnessButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getBrightnessScreen());
            }
        });
        humidityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getHumidityScreen());
            }
        });
        babyStateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getBabyStateScreen());
            }
        });
        settingsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getSettingsScreen());
            }
        });
        helpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	changeScreen(mActivity.getHelpScreen());
            }
        });
 
		// Add buttons in HomeScreen
        this.mActivity.getHomeScreen().setHomeButton(homeButton);
        this.mActivity.getHomeScreen().setTempButton(tempButton);
        this.mActivity.getHomeScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getHomeScreen().setHumidityButton(humidityButton);
        this.mActivity.getHomeScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getHomeScreen().setSettingsButton(settingsButton);
        this.mActivity.getHomeScreen().setHelpButton(helpButton);
		

        // Add buttons in TempScreen
        this.mActivity.getTempScreen().setHomeButton(homeButton);
        this.mActivity.getTempScreen().setTempButton(tempButton);
        this.mActivity.getTempScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getTempScreen().setHumidityButton(humidityButton);
        this.mActivity.getTempScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getTempScreen().setSettingsButton(settingsButton);
        this.mActivity.getTempScreen().setHelpButton(helpButton);

		
        // Add buttons in BrightnessScreen
        this.mActivity.getBrightnessScreen().setHomeButton(homeButton);
        this.mActivity.getBrightnessScreen().setTempButton(tempButton);
        this.mActivity.getBrightnessScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getBrightnessScreen().setHumidityButton(humidityButton);
        this.mActivity.getBrightnessScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getBrightnessScreen().setSettingsButton(settingsButton);
        this.mActivity.getBrightnessScreen().setHelpButton(helpButton);
        
       // Add buttons in HumidityScreen
        this.mActivity.getHumidityScreen().setHomeButton(homeButton);
        this.mActivity.getHumidityScreen().setTempButton(tempButton);
        this.mActivity.getHumidityScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getHumidityScreen().setHumidityButton(humidityButton);
        this.mActivity.getHumidityScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getHumidityScreen().setSettingsButton(settingsButton);
        this.mActivity.getHumidityScreen().setHelpButton(helpButton);

        // Add buttons in BabyStateScreen
        this.mActivity.getBabyStateScreen().setHomeButton(homeButton);
        this.mActivity.getBabyStateScreen().setTempButton(tempButton);
        this.mActivity.getBabyStateScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getBabyStateScreen().setHumidityButton(humidityButton);
        this.mActivity.getBabyStateScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getBabyStateScreen().setSettingsButton(settingsButton);
        this.mActivity.getBabyStateScreen().setHelpButton(helpButton);
        
        // Add buttons in SettingsScreen
        this.mActivity.getSettingsScreen().setHomeButton(homeButton);
        this.mActivity.getSettingsScreen().setTempButton(tempButton);
        this.mActivity.getSettingsScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getSettingsScreen().setHumidityButton(humidityButton);
        this.mActivity.getSettingsScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getSettingsScreen().setSettingsButton(settingsButton);
        this.mActivity.getSettingsScreen().setHelpButton(helpButton);

        // Add buttons in HelpScreen
        this.mActivity.getHelpScreen().setHomeButton(homeButton);
        this.mActivity.getHelpScreen().setTempButton(tempButton);
        this.mActivity.getHelpScreen().setBrightnessButton(brightnessButton);
        this.mActivity.getHelpScreen().setHumidityButton(humidityButton);
        this.mActivity.getHelpScreen().setBabyStateButton(babyStateButton);
        this.mActivity.getHelpScreen().setSettingsButton(settingsButton);
        this.mActivity.getHelpScreen().setHelpButton(helpButton);
    }

    /**
     * @brief Close the current view and display the new one.
     */
    private void changeScreen(Screen newScreen){
	    this.mActivity.getCurrentScreen().close();
	    this.mActivity.setCurrentScreen(newScreen);
	    displayCurrentView();
    }

    /**
     * @brief Display the current screen.
     */
    public void displayCurrentView(){
        this.mActivity.getCurrentScreen().display();
    }

    /*********** ACCESSORS ***********/
	public MainActivity getmActivity() {
		return mActivity;
	}

	public void setmActivity(MainActivity mActivity) {
		this.mActivity = mActivity;
	}

    public CriticalityThresholds getCriticalityThresholds() {
        return criticalityThresholds;
    }
}
